package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Attendance;
import com.qianfeng.entity.Employee;
import com.qianfeng.entity.WorkType;
import com.qianfeng.mapper.AttendanceMapper;
import com.qianfeng.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Transactional
    @Override
    public void addAttendance(Attendance attendance) {
        attendance.setEmployee(new Employee(attendance.getEmpId()));
        attendance.setWorkType(new WorkType(attendance.getWorkTypeId()));
        attendanceMapper.addAttendance(attendance);
    }

    @Transactional
    @Override
    public void deleteAttendance(Integer attendanceId) {
        attendanceMapper.deleteAttendance(attendanceId);
    }

    @Transactional
    @Override
    public void updateAttendance(Attendance attendance) {
        attendance.setEmployee(new Employee(attendance.getEmpId()));
        attendance.setWorkType(new WorkType(attendance.getWorkTypeId()));
        attendanceMapper.updateAttendance(attendance);
    }

    @Transactional(readOnly = true)
    @Override
    public Page getAttendancePage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo, pageSize);
        List<Attendance> attendanceList = attendanceMapper.getAttendanceList();
        PageInfo<Attendance> info = new PageInfo<>(attendanceList);
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setHasPre(info.isHasPreviousPage());
        page.setHasNext(info.isHasNextPage());
        page.setPage(info.getList());
        return page;
    }

    @Transactional(readOnly = true)
    @Override
    public Attendance getAttendanceByAttendanceId(Integer attendanceId) {
        return attendanceMapper.getAttendanceByAttendanceId(attendanceId);
    }
}
